package com.wondertek.poms.service.controller;

import com.wondertek.poms.core.service.IPrdContService;
import com.wondertek.poms.dao.po.PrdCont;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.domain.Example;

import javax.annotation.Resource;
import java.util.Optional;


@SuppressWarnings("DuplicatedCode")
@Slf4j
public abstract class BasePrdContController {
    @Resource
    private IPrdContService prdContService;

    protected void changePrdContPubStatusRecall(Long prdContId) {

        log.info("节目撤回成功");
        //修改节目表发布状态字段
        PrdCont prdContParam = new PrdCont();
        prdContParam.setPrdContId(prdContId);
        Optional<PrdCont> prdContOptional = prdContService.findOne(Example.of(prdContParam));
        //只有当节目ID在表中存在的时候才会操作
        prdContOptional.ifPresent(prdCont -> {
            prdCont.setPubStatus(PrdCont.PUBOFF_STATUS_SUCCESS);
            prdContService.merge(prdCont);
        });

    }

    protected void changePrdContPubStatusPublish(Long prdContId) {

        log.info("节目发布成功");
        //修改节目表发布状态字段
        PrdCont prdContParam = new PrdCont();
        prdContParam.setPrdContId(prdContId);
        Optional<PrdCont> prdContOptional = prdContService.findOne(Example.of(prdContParam));
        //只有当节目ID在表中存在的时候才会操作
        prdContOptional.ifPresent(prdCont -> {
            prdCont.setPubStatus(PrdCont.PUBON_STATUS_SUCCESS);
            prdContService.merge(prdCont);
        });


    }

    /**
     * 手动增加计费配置
     * 根据节目ID设置节目表的是否人工添加字段
     *
     * @param prdContId 节目ID
     */
    protected void changePubStatusPublishAndIsPersonOperation(Long prdContId) {
        PrdCont param = new PrdCont();
        param.setPrdContId(prdContId);
        Optional<PrdCont> one = prdContService.findOne(Example.of(param));
        one.ifPresent(prdCont -> {
            prdCont.setIsPersonOperation("1");
            prdCont.setPubStatus(PrdCont.PUBON_STATUS_SUCCESS);
            prdContService.merge(prdCont);
        });
    }

    /**
     * 节目表人工操作标志字段修改
     * <p>
     * 手动计费配置新增
     * 手动计费配置删除
     *
     * @param prdContId 节目ID
     */
    protected void changeIsPersonOperation(Long prdContId) {
        PrdCont param = new PrdCont();
        param.setPrdContId(prdContId);
        Optional<PrdCont> one = prdContService.findOne(Example.of(param));
        one.ifPresent(prdCont -> {
            prdCont.setIsPersonOperation("1");
            prdContService.merge(prdCont);
        });
    }


}
